﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Jy123.BLL.Public;

namespace Jy123.BLL.User
{
    public class Letter
    {
        /// <summary>
        /// 分页返回收件箱
        /// </summary>
        /// <param name="userId">当前用户的ID</param>
        /// <param name="currentpage">当前页</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="field">查询的字段</param>
        /// <param name="sortField">排序字段</param>
        /// <param name="order">排序方式(true为ASC|false为DESC)</param>
        /// <returns>收信列表</returns>
        public DataSet PaginationLetterList_In(int userId, int currentpage, int pageSize, string field, string sortField)
        {
            Jy123.Model.MPagination mPagination = new Jy123.Model.MPagination();
            StringBuilder strFilter = new StringBuilder();
            strFilter.AppendFormat("(ID IN (SELECT FLetter FROM User_Letterinbox WHERE (FUser = {0})))", userId);

            mPagination.Tables = "V_User_LetterList";
            mPagination.PrimaryKey = "ID";
            mPagination.Fields = field;
            mPagination.CurrentPage = currentpage;
            mPagination.Filter = strFilter.ToString();
            mPagination.PageSize = pageSize;
            mPagination.Sort = sortField;
            mPagination.Group = string.Empty;


            return Pagination.GetList(mPagination);
        }

        /// <summary>
        /// 分页返回发件箱
        /// </summary>
        /// <param name="userId">当前用户的ID</param>
        /// <param name="currentpage">当前页</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="field">查询的字段</param>
        /// <param name="sortField">排序字段</param>
        /// <param name="order">排序方式(true为ASC|false为DESC)</param>
        /// <returns>发件列表</returns>
        public DataSet PaginationLetterList_Out(int userId, int currentpage, int pageSize, string field, string sortField)
        {
            Jy123.Model.MPagination mPagination = new Jy123.Model.MPagination();
            StringBuilder strFilter = new StringBuilder();
            strFilter.AppendFormat("(ID IN (SELECT FLetter FROM User_LetterOutbox WHERE (FUser = {0})))", userId);

            mPagination.Tables = "V_User_LetterList";
            mPagination.PrimaryKey = "ID";
            mPagination.Fields = field;
            mPagination.CurrentPage = currentpage;
            mPagination.Filter = strFilter.ToString();
            mPagination.PageSize = pageSize;
            mPagination.Sort = sortField;
            mPagination.Group = string.Empty;

            return Pagination.GetList(mPagination);
        }

        ///// <summary>
        ///// 返回站内信列表
        ///// </summary>
        ///// <param name="userId">用户ID</param>
        ///// <param name="currentpage">当前页</param>
        ///// <param name="pageSize">页大小</param>
        ///// <param name="field">查询的字段</param>
        ///// <param name="sortField">排序字段</param>
        ///// <returns>最新收信列表</returns>
        //public DataSet LetterList(int userId, int currentpage, int pageSize, string field, string sortField)
        //{
        //    Jy123.Model.MPagination mPagination = new Jy123.Model.MPagination();
        //    StringBuilder strFilter = new StringBuilder();
        //    strFilter.AppendFormat("ID IN (SELECT FLetter FROM User_Letterinbox WHERE (FUserReceiver = {0}) and isread is null)", userId);

        //    mPagination.Tables = "V_User_LetterList";
        //    mPagination.PrimaryKey = "ID";
        //    mPagination.Fields = field;
        //    mPagination.CurrentPage = currentpage;
        //    mPagination.Filter = strFilter.ToString();
        //    mPagination.PageSize = pageSize;
        //    mPagination.Sort = sortField;

        //    return Pagination.GetList(mPagination);
        //}

        /// <summary>
        /// 统计最新未阅读的信
        /// </summary>
        /// <param name="userID">当前用户ID </param>
        /// <returns>数量</returns>
        public int CountNewLetter(int userID)
        {
            StringBuilder strSql = new StringBuilder();
            //strSql.Append("select count(ID) from dbo.V_User_LetterList where ID IN (SELECT FLetter FROM User_Letterinbox WHERE (FUser = @UserID) and isread is null)");
            strSql.Append("select count(ID) from dbo.V_User_LetterLists where FUserReceiver =@UserID and isread=0 and (IsDel=0 or IsDel=1)  and LtType=1");
            SqlParameter[] parameters = { 
                                        new SqlParameter("@UserID", SqlDbType.Int, 4)};
            parameters[0].Value = userID;
            return (int)DBUtility.DbHelperSQL.GetSingle(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 统计最新未读通知
        /// </summary>
        /// <param name="userID">用户ID</param>
        /// <returns>数量</returns>
        public int CountNewNotice()
        {
            StringBuilder strSql = new StringBuilder();
            //strSql.Append("select count(ID) from dbo.V_User_LetterList where ID IN (SELECT FLetter FROM User_Letterinbox WHERE (FUser = @UserID) and isread is null)");
            strSql.Append("select count(ID) from dbo.V_User_LetterLists where  isread=0 and IsDel<>2 and LtType=0");
            return (int)DBUtility.DbHelperSQL.GetSingle(strSql.ToString());
        }
        /// <summary>
        /// 获得站内信列表
        /// </summary>
        /// <param name="cpage">起始页</param>
        /// <param name="psize">页大小</param>
        /// <param name="strWhere">筛选条件</param>
        /// <param name="strSort">排序方式</param>
        /// <param name="counts">总数[0],页数[1]</param>
        /// <returns>数据集</returns>
        public DataSet LetterList(int cpage, int psize, string strWhere, string strSort, out List<int?> counts)
        {
            return Pagination.GetList(new Jy123.Model.MPagination()
            {
                Filter = (string.IsNullOrEmpty(strWhere)) ? null : strWhere,
                CurrentPage = cpage,
                Fields = "*",
                PageSize = psize,
                PrimaryKey = "ID",
                Sort = strSort,
                Tables = "V_User_LetterLists"
            }, out counts);
        }

        /// <summary>
        /// 站内信评论列表
        /// </summary>
        /// <param name="cpage">起始页</param>
        /// <param name="psize">页大小</param>
        /// <param name="strWhere">筛选条件</param>
        /// <param name="strSort">排序方式</param>
        /// <param name="counts">总数[0],页数[1]</param>
        /// <returns>数据集</returns>
        public DataSet LetterCommentList(int cpage, int psize, string strWhere, string strSort, out List<int?> counts)
        {
            return Pagination.GetList(new Jy123.Model.MPagination()
            {
                Filter = (string.IsNullOrEmpty(strWhere)) ? null : strWhere,
                CurrentPage = cpage,
                Fields = "*",
                PageSize = psize,
                PrimaryKey = "ID",
                Sort = strSort,
                Tables = "V_User_LetterComment"
            }, out counts);
        }

        /// <summary>
        /// 统计站内信数量
        /// </summary>
        /// <param name="userID">用户ID</param>
        /// <returns>数量</returns>
        public int LetterCount(int userID)
        {
            int count = 0;
            string strSql = "select Count(ID)as num from dbo.V_User_LetterLists where Fuserreceiver=@UserID and IsDel<>2";
            SqlParameter[] parameters = {
                                        new SqlParameter("@UserID",SqlDbType.Int)};
            parameters[0].Value = userID;
            count = (int)DBUtility.DbHelperSQL.GetSingle(strSql,parameters);
            return count;
        }
        /// <summary>
        /// 未读信
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public int LetterCountNoRead(int userID)
        {
            int count = 0;
            string strSql = "select Count(ID)as num from dbo.V_User_LetterLists where Fuserreceiver=@UserID and isread=0 and IsDel<>2";
            SqlParameter[] parameters = {
                                        new SqlParameter("@UserID",SqlDbType.Int)};
            parameters[0].Value = userID;
            count = (int)DBUtility.DbHelperSQL.GetSingle(strSql, parameters);
            return count;
        }
        /// <summary>
        /// 已读信
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public int LetterCountReaded(int userID)
        {
            int count;
            string strSql = "select Count(ID)as num from dbo.V_User_LetterLists where Fuserreceiver=@UserID and isread=1 and IsDel<>2";
            SqlParameter[] parameters = {
                                        new SqlParameter("@UserID",SqlDbType.Int)};
            parameters[0].Value = userID;
            count = (int)DBUtility.DbHelperSQL.GetSingle(strSql, parameters);
            return count;
        }
        //public int LetterCommentCount(int userID)
        //{
        //    int count;
        //    string strSql = "";
        //}
    }
}
